package com.nullpointer.xchat.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.nullpointer.xchat.R;
import com.nullpointer.xchat.ViewImageActivity;
import com.nullpointer.xchat.object.MessageContent;
import com.nullpointer.xchat.object.MessageContent.MsgType;
import com.nullpointer.xchat.utils.Common;
import com.nullpointer.xchat.utils.DataManager;
import com.nullpointer.xchat.utils.Emoticons;

public class ChatRoomAdapter extends ArrayAdapter<MessageContent> {

	private static class ViewHolder {
		public final LinearLayout rootView;
		public final LinearLayout llLeftChat;
		public final TextView tvLeftName;
		public final ImageView ivLeftAvatar;
		public final TextView tvLeftMessage;
		public final ImageView ivLeftImage;
		public final LinearLayout llRightChat;
		public final TextView tvRightName;
		public final TextView tvRightMessage;
		public final ImageView ivRightAvatar;
		public final ImageView ivRightImage;

		private ViewHolder(LinearLayout rootView, LinearLayout llLeftChat,
				TextView tvLeftName, ImageView ivLeftAvatar,
				TextView tvLeftMessage, ImageView ivLeftImage,
				LinearLayout llRightChat, TextView tvRightName,
				TextView tvRightMessage, ImageView ivRightAvatar,
				ImageView ivRightImage) {
			this.rootView = rootView;
			this.llLeftChat = llLeftChat;
			this.tvLeftName = tvLeftName;
			this.ivLeftAvatar = ivLeftAvatar;
			this.tvLeftMessage = tvLeftMessage;
			this.ivLeftImage = ivLeftImage;
			this.llRightChat = llRightChat;
			this.tvRightName = tvRightName;
			this.tvRightMessage = tvRightMessage;
			this.ivRightAvatar = ivRightAvatar;
			this.ivRightImage = ivRightImage;
		}

		public static ViewHolder create(LinearLayout rootView) {
			LinearLayout llLeftChat = (LinearLayout) rootView
					.findViewById(R.id.ll_left_chat);
			TextView tvLeftName = (TextView) rootView
					.findViewById(R.id.tv_left_name);
			ImageView ivLeftAvatar = (ImageView) rootView
					.findViewById(R.id.iv_left_avatar);
			TextView tvLeftMessage = (TextView) rootView
					.findViewById(R.id.tv_left_message);
			ImageView ivLeftImage = (ImageView) rootView
					.findViewById(R.id.iv_left_image);
			LinearLayout llRightChat = (LinearLayout) rootView
					.findViewById(R.id.ll_right_chat);
			TextView tvRightName = (TextView) rootView
					.findViewById(R.id.tv_right_name);
			TextView tvRightMessage = (TextView) rootView
					.findViewById(R.id.tv_right_message);
			ImageView ivRightAvatar = (ImageView) rootView
					.findViewById(R.id.iv_right_avatar);
			ImageView ivRightImage = (ImageView) rootView
					.findViewById(R.id.iv_right_image);
			return new ViewHolder(rootView, llLeftChat, tvLeftName,
					ivLeftAvatar, tvLeftMessage, ivLeftImage, llRightChat,
					tvRightName, tvRightMessage, ivRightAvatar, ivRightImage);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater.inflate(R.layout.item_lv_chat_room, parent,
					false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		AQuery aq = new AQuery(convertView);
		MessageContent item = getItem(position);
		Integer userID = item.getFromUserID();
		if (userID.equals(DataManager.getInstance(context).getUserID())) {
			// Right content
			vh.llLeftChat.setVisibility(View.GONE);
			vh.llRightChat.setVisibility(View.VISIBLE);
			setViewContent(item, vh.tvRightName, vh.tvRightMessage,
					vh.ivRightAvatar, vh.ivRightImage, aq);
		} else {
			// Left content
			vh.llRightChat.setVisibility(View.GONE);
			vh.llLeftChat.setVisibility(View.VISIBLE);
			setViewContent(item, vh.tvLeftName, vh.tvLeftMessage,
					vh.ivLeftAvatar, vh.ivLeftImage, aq);
		}
		return vh.rootView;
	}

	private void setViewContent(final MessageContent item, TextView tvName,
			TextView tvMessage, ImageView ivAvatar, ImageView ivImage, AQuery aq) {
		tvName.setText(item.getName());
		if (item.getAvartar() != null) {
			ImageOptions options = new ImageOptions();
			options.round = 5;
			options.fileCache = true;
			options.memCache = true;
			options.targetWidth = 48;
			// options.fallback =
			aq.id(ivAvatar).image(item.getAvartar(), options);
			ivAvatar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, ViewImageActivity.class);
					intent.putExtra(Common.ExtraKey.IMAGE_URL,
							item.getAvartar());
					context.startActivity(intent);
				}
			});
		} else {
			ivAvatar.setImageResource(R.drawable.ic_no_avatar);
		}

		if (item.getMsgType() == MsgType.IMAGE) {
			tvMessage.setText("Tap to view picture!");
			ivImage.setVisibility(View.VISIBLE);
			if (item.getContent() != null) {
				ivImage.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context,
								ViewImageActivity.class);
						intent.putExtra(Common.ExtraKey.IMAGE_URL,
								item.getContent());
						context.startActivity(intent);
					}
				});
			}
		} else {
			//tvMessage.setVisibility(View.VISIBLE);
			ivImage.setVisibility(View.GONE);
			tvMessage.setText(Emoticons.getSmiledText(context,
					item.getContent()));
		}
	}

	private LayoutInflater mInflater;
	private Context context;

	// Constructors
	public ChatRoomAdapter(Context context, List<MessageContent> objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public ChatRoomAdapter(Context context, MessageContent[] objects) {
		super(context, 0, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

}
